package org.ww.order.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.ww.order.entity.OrderEntity;
import org.ww.order.entity.OrderReturnReasonEntity;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

/**
 * @program: gmall
 * @description:
 * @author: ww
 * @create: 2024-10-28 11:14
 **/
@Slf4j
@RestController
public class RabbitmqTestController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/sendMsg")
    public void sendMsg(@RequestParam(value = "num", defaultValue = "10") Integer num){
        for (int i = 0; i < num; i++) {
            if(i%2 == 0){
                OrderEntity entity = new OrderEntity();
                entity.setBillContent("orderEntity"+i);
                entity.setAutoConfirmDay(222);
                entity.setBillHeader("header");
                entity.setBillReceiverEmail("email");
                entity.setBillType(1);
                entity.setCommentTime(new Date());
                entity.setConfirmStatus(3);
                entity.setCouponAmount(new BigDecimal(10));
                //rabbitmq没有采用json的形式进行发送，需要配置json的序列化形式（RabbitmqConfig)
                rabbitTemplate.convertAndSend("hello-java-exchange","hello-java",entity,new CorrelationData(UUID.randomUUID().toString()));
                log.info("消息发送成功:{}",i);
            }else{
                OrderReturnReasonEntity returnReasonEntity = new OrderReturnReasonEntity();
                returnReasonEntity.setName("returnReasonEntity"+i);
                returnReasonEntity.setCreateTime(new Date());
                returnReasonEntity.setStatus(i);
                rabbitTemplate.convertAndSend("hello-java-exchange","hello22-java",returnReasonEntity,new CorrelationData(UUID.randomUUID().toString()));
                log.info("消息发送成功:{}",i);
            }
        }
    }

}
